<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\cache\PositionModule\ClosePosition;
use Doraemon\cache\UserNoter;
use Doraemon\service\position\AdmExtOperationModule;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ImgUrl;

class HomeRecommendGuide extends BaseModule
{

    public function extListMap($ids)
    {
        return empty($ids) ? [] : AdmExtOperationModule::getPositionGuideTool($ids);
    }

    public function getExtInfo($baseInfo)
    {
        $id = $baseInfo['id'] ?? 0;
        return $id > 0 ? AdmExtOperationModule::getPositionGuideTool($id) : [];
    }

    public function extCheckActive($v, $extInput)
    {
        $type = $v['position_id'] ?? 0;
        $uid = HhzClient::getUid();
        $singleCheck = ClosePosition::callShow($uid, 'r', $type, $v['id']);
        if ($singleCheck) return false;

        //查询满足条件的规则
        $ext = $v['ext'];

        //查询用户使用app功能最后时间
        $time = UserNoter::callUsed($uid, $ext['apply'], 'r');

        if ($ext['days'] > 0) {//大于多少天没有使用
            $check = is_null($time) ? true : $ext['days'] * 86400 <= (time() - $time);
        } else {//没有使用过
            $check = is_null($time) || ((time() - $time) < 36000);
        }

        return $check;
    }

    public function format($hit, $uid, $userProfileExt, $statSignExt)
    {
        if ($hit) {
            $ext = $hit['ext'] ?? [];
            $type = $hit['position_id'] ?? 0;
            $response = [
                'title' => $ext['title'] ?? '',
                'subtitle' => $ext['subtitle'] ?? '',
                'icon' => empty($ext['icon']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $ext['icon'], ImgUrl::IMAGE_SIZE_OP_ORI),
                'button_title' => $ext['button_title'] ?? '',
                'link' => $this->generateLink($hit['link_type'], $hit['link_params']),
                'bgcolor' => $ext['bgcolor'] ?? '',
                'statSign' => $this->statSign($hit, $statSignExt),
            ];

            //todo 关闭显示过的配置
//            ClosePosition::callShow($uid, 'w', $type, $hit['id']);
        }

        return empty($response) ? null : $response;
    }
}